home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalBumps.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  220 lines

  1. /*   
  2.  * @(#)MetalBumps.java    1.12 98/03/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.*;
  24. import com.sun.java.swing.*;
  25. import java.io.*;
  26. import java.util.*;
  27.  
  28. /**
  29.  * Implements the Metal Look and Feel.
  30.  * 
  31.  * @version 1.12 03/05/98
  32.  * @author Tom Santos
  33.  */
  34.  
  35.  
  36. class MetalBumps implements Icon, Serializable {
  37.  
  38.     protected int xBumps;
  39.     protected int yBumps;
  40.     protected Color topColor = MetalLookAndFeel.getPrimaryControlHighlight();
  41.     protected Color shadowColor = MetalLookAndFeel.getPrimaryControlDarkShadow();
  42.     protected Color backColor = MetalLookAndFeel.getPrimaryControlShadow();
  43.  
  44.     protected static Vector buffers = new Vector();
  45.     protected BumpBuffer buffer;
  46.     
  47.     public MetalBumps( Dimension bumpArea ) {
  48.         this( bumpArea.width, bumpArea.height );
  49.     }
  50.  
  51.     public MetalBumps( int width, int height ) {
  52.         setBumpArea( width, height );
  53.     buffer = getBuffer( topColor, shadowColor, backColor );
  54.     if ( buffer == null ) {
  55.         createBuffer();
  56.     }
  57.     }
  58.  
  59.     public MetalBumps( int width, int height,
  60.                Color newTopColor, Color newShadowColor, Color newBackColor ) {
  61.         setBumpArea( width, height );
  62.     setBumpColors( newTopColor, newShadowColor, newBackColor );
  63.     buffer = getBuffer( topColor, shadowColor, backColor );
  64.     if ( buffer == null ) {
  65.         createBuffer();
  66.     }
  67.     }
  68.  
  69.     protected void createBuffer() {
  70.         buffer = new BumpBuffer( topColor, shadowColor, backColor );
  71.     buffers.addElement( buffer );
  72.     }
  73.  
  74.     protected BumpBuffer getBuffer( Color aTopColor, Color aShadowColor, Color aBackColor ) {
  75.         BumpBuffer result = null;
  76.  
  77.         Enumeration elements = buffers.elements();
  78.  
  79.     while ( elements.hasMoreElements() ) {
  80.         BumpBuffer aBuffer = (BumpBuffer)elements.nextElement();
  81.         if ( aBuffer.hasSameColors( aTopColor, aShadowColor, aBackColor ) ) {
  82.             result = aBuffer;
  83.         break;
  84.         } 
  85.     }
  86.  
  87.     return result;
  88.     }
  89.  
  90.     public void setBumpArea( Dimension bumpArea ) {
  91.         setBumpArea( bumpArea.width, bumpArea.height );
  92.     }
  93.  
  94.     public void setBumpArea( int width, int height ) {
  95.         xBumps = width / 2;
  96.     yBumps = height / 2;
  97.     }
  98.  
  99.     public void setBumpColors( Color newTopColor, Color newShadowColor, Color newBackColor ) {
  100.         topColor = newTopColor;
  101.     shadowColor = newShadowColor;
  102.     backColor = newBackColor;
  103.     buffer = getBuffer( topColor, shadowColor, backColor );
  104.     if ( buffer == null ) {
  105.         createBuffer();
  106.     }
  107.     }
  108.  
  109.     public void paintIcon( Component c, Graphics g, int x, int y ) {
  110.     Rectangle oldClip = g.getClipBounds();
  111.     Rectangle desiredClip = new Rectangle( x, y, getIconWidth(), getIconHeight() );
  112.     Rectangle clip = desiredClip.intersection(oldClip);
  113.     g.setClip( clip.x, clip.y, clip.width, clip.height);
  114.  
  115.     int bufferWidth = buffer.getImageSize().width;
  116.     int bufferHeight = buffer.getImageSize().height;
  117.     int xTiles = (getIconWidth() / bufferWidth) + 1;
  118.     int yTiles = (getIconHeight() / bufferHeight) + 1;
  119.  
  120.     for ( int row = 0; row < yTiles; ++row ) {
  121.         for ( int column = 0; column < xTiles; ++column ) {
  122.             g.drawImage( buffer.getImage(), x + (column * bufferWidth), y + (row * bufferHeight), null );
  123.         }
  124.     }
  125.  
  126.     g.setClip( oldClip.x, oldClip.y, oldClip.width, oldClip.height );
  127.     }
  128.  
  129.     public int getIconWidth() {
  130.         return xBumps * 2;
  131.     }
  132.  
  133.     public int getIconHeight() {
  134.         return yBumps * 2;
  135.     }
  136. }
  137.  
  138.  
  139. class BumpBuffer implements Serializable {
  140.  
  141.     static Frame frame;
  142.     static Component component;
  143.  
  144.     static final int IMAGE_SIZE = 100;
  145.     static Dimension imageSize = new Dimension( IMAGE_SIZE, IMAGE_SIZE );
  146.  
  147.     transient Image image;
  148.     Color topColor;
  149.     Color shadowColor;
  150.     Color backColor;
  151.  
  152.     public BumpBuffer( Color aTopColor, Color aShadowColor, Color aBackColor ) {
  153.         createComponent();
  154.         image = getComponent().createImage( IMAGE_SIZE, IMAGE_SIZE );
  155.     topColor = aTopColor;
  156.     shadowColor = aShadowColor;
  157.     backColor = aBackColor;
  158.     fillBumpBuffer();
  159.     }
  160.  
  161.     public boolean hasSameColors( Color aTopColor, Color aShadowColor, Color aBackColor ) {
  162.     return topColor.equals( aTopColor )       &&
  163.            shadowColor.equals( aShadowColor ) &&
  164.            backColor.equals( aBackColor );
  165.     }
  166.  
  167.     public Image getImage() {
  168.         if (image == null) {
  169.         image = getComponent().createImage( IMAGE_SIZE, IMAGE_SIZE );
  170.         fillBumpBuffer();
  171.     }
  172.         return image;
  173.     }
  174.  
  175.     public Dimension getImageSize() {
  176.         return imageSize;
  177.     }
  178.  
  179.     protected void fillBumpBuffer() {
  180.         Graphics g = image.getGraphics();
  181.  
  182.     g.setColor( backColor );
  183.     g.fillRect( 0, 0, IMAGE_SIZE, IMAGE_SIZE );
  184.     
  185.     int columnX = 0;
  186.     int xBumps = IMAGE_SIZE / 2;
  187.  
  188.         for ( int i = 0; i < xBumps; ++i, columnX += 2 ) {
  189.         paintColumn( g, columnX, i % 2 == 0 ? 0 : 2 );
  190.     }
  191.     }
  192.  
  193.     protected void paintColumn( Graphics g, int x, int y ) {
  194.         while ( y <= IMAGE_SIZE - 1 ) {
  195.         g.setColor( topColor );
  196.         g.drawLine( x, y, x, y );
  197.  
  198.         g.setColor( shadowColor );
  199.         g.drawLine( x + 1, y + 1, x + 1, y + 1 );
  200.  
  201.         y += 4;
  202.     }
  203.     }
  204.   
  205.     protected Component getComponent() {return component;}
  206.  
  207.     protected void createComponent() {
  208.         if (frame == null) {
  209.         frame = new Frame( "bufferCreator" );
  210.     }
  211.  
  212.     if (component == null ) {
  213.         component = new Canvas();
  214.         frame.add( component, BorderLayout.CENTER );
  215.         frame.addNotify();
  216.     }
  217.     }
  218.  
  219. }
  220.